<template>
  <div class="page-search">
    <i-form v-bind="IFormConfig" v-model="formData">
      <template #footer>
        <div class="handle-btns">
          <el-button icon="el-icon-refresh" @click="handleResetClick">重置</el-button>
          <el-button type="primary" icon="el-icon-search" @click="handleQueryClick">搜索</el-button>
        </div>
      </template>
    </i-form>
  </div>
</template>
<script lang="ts">
import { defineComponent, ref } from 'vue'
import IForm from '@/base-ui/form/index'
export default defineComponent({
  props: {
    IFormConfig: {
      type: Object,
      required: true
    }
  },
  components: { IForm },
  emits: ['handleResetClick', 'handleQueryClick'],
  setup(props, { emit }) {
    // 双向绑定的属性是由配置文件的field来决定
    const formItems = props.IFormConfig?.formItems ?? []
    const formOriginData: any = {}
    for (const item of formItems) {
      formOriginData[item.filed] = ''
    }
    const formData = ref(formOriginData)

    // 重置搜索条件
    const handleResetClick = () => {
      for (const key in formOriginData) {
        formData.value[key] = formOriginData[key]
      }
      emit('handleResetClick')
    }

    // 条件查询
    const handleQueryClick = () => {
      emit('handleQueryClick', formData.value)
    }
    return {
      formData,
      handleResetClick,
      handleQueryClick
    }
  }
})
</script>
<style lang="less" scoped>
.handle-btns {
  text-align: right;
  padding: 0 50px 20px 0;
}
</style>
